package twoD.IsoPfem.EmbeddedCrackModel;

import inf.jlinalg.Vector3D;
import inf.v3d.obj.Line;
import inf.v3d.view.Viewer;
import oneD.fem.algorithm.XLine;
import twoD.IsoPfem.Visualizer;

public class CrackVisualizer extends Visualizer {

	private CrackStructure cStruct_;

	public CrackVisualizer(CrackStructure cStruct, Viewer view) {
		super(cStruct, view);
		cStruct_ = cStruct;
	}

	public void drawCrack() {
		for (int i = 0; i < cStruct_.getNumberOfElements(); i++) {
			if (cStruct_.getElements(i).isCrack()) {
				double[] tmp = cStruct_.getElements(i)
						.checkCrackPositiveDirection();
				int n = ((int) tmp[2]) / 2;
				int[] v = new int[2];
				if (n == 0) {
					v[0] = 1;
					v[1] = 2;
				} else if (n == 1) {
					v[0] = 0;
					v[1] = 2;
				} else if (n == 2) {
					v[0] = 0;
					v[1] = 1;
				}
				Vector3D cog = cStruct_.getElements(i).getCog();
				double d = cStruct_.getElements(i).getDiameter();
				XLine crackLine = new XLine(tmp[0], tmp[1], cog.getX1(),
						cog.getX2());
				XLine l1 = new XLine(cStruct_.getElements(i).getNodes(n)
						.getPosition(), cStruct_.getElements(i).getNodes(v[0])
						.getPosition());
				XLine l2 = new XLine(cStruct_.getElements(i).getNodes(n)
						.getPosition(), cStruct_.getElements(i).getNodes(v[1])
						.getPosition());
				double[] p1 = crackLine.getIntersection(l1);
				double[] p2 = crackLine.getIntersection(l2);
				Line L = new Line(p1[0], p1[1], 0.0, p2[0], p2[1], 0.0);
				L.setColor("red");
				getView().addObject3D(L);
				Line L1 = new Line(cog.getX1(), cog.getX2(), 0.0, cog.getX1()
						+ tmp[0] * d / 3, cog.getX2() + tmp[1] * d / 3, 0.0);
				getView().addObject3D(L1);
			}
		}
	}
}
